<?php
/**
 * 九天云短信平台 支持自定义短信内容
 */

namespace app\service\SmsService;

class NineDayCloud extends Driver
{
    private static $instance;

    private $config = [
        'action' => 'send',
        'app_key' => '',
        'app_secret' => '',
        'app_extno' => '',
        'sms_sign' => '',
        'url' => 'http://122.112.185.255:7862/sms',
    ];

    public function __construct(array $config = [])
    {
        if (isset($config['api_key'])) $this->config['app_key'] = $config['api_key'];
        if (isset($config['api_secret'])) $this->config['app_secret'] = $config['api_secret'];
        if (isset($config['sms_sign'])) $this->config['sms_sign'] = $config['sms_sign'];
        if (isset($config['api_extno'])) $this->config['app_extno'] = $config['api_extno'];
    }

    static function instance(array $config = [])
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self($config);
        }
        return self::$instance;
    }

    public function sendSingle($mobile, $content, $params = [])
    {
        $res = $this->sendRequest($mobile, $content);
        if ($res['status'] != STATUS_CODE_SUCCESS) {
            return $res;
        }
        return funReturn(STATUS_CODE_SUCCESS, 'success');
    }


    private function sendRequest($mobile, $msgContent)
    {
        $data = [
            'action' => $this->config['action'],
            'account' => $this->config['app_key'],
            'password' => $this->config['app_secret'],
            'mobile' => $mobile,
            'content' => '【' . $this->config['sms_sign'] . '】' . $msgContent,
            'extno' => $this->config['app_extno'],
            'rt' => 'json',
        ];

        $response = $this->httpPost($this->config['url'], $data);
        $response = json_decode($response, true);
        if (!$response) return funReturn(STATUS_CODE_INSIDEERROR, '短信平台接口请求失败');
        if (isset($response['status']) && $response['status'] == '0') {
            return funReturn(STATUS_CODE_SUCCESS, 'success');
        } else {
            return funReturn(STATUS_CODE_DATAERROR, '短信平台接口请求失败');
        }
    }


    private function httpPost($url, $param, $post_file = false)
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1);
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $strPOST = http_build_query($param);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }
}
